package com.wmssystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wmssystem.dto.Result;
import com.wmssystem.entity.MaterialDetailMaintenance;
import com.wmssystem.entity.OuterDetail;
import com.wmssystem.entity.ReceiveMaterial;
import com.wmssystem.service.impl.MaterialDetailMaintenanceService;
import com.wmssystem.service.impl.OuterDetailService;
import com.wmssystem.service.impl.ReceiveMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("Receive")
@ResponseBody
public class Receive {
    @Autowired
    ReceiveMaterialService receiveMaterialService;
    @Autowired
    OuterDetailService outerDetailService;

    @Autowired
    MaterialDetailMaintenanceService materialDetailMaintenanceService;

    /**
     * 扫描LPN码，获取外层明细信息
     *
     * @param LPN 由于大小写问题，注解使用@RequestParam11
     * @return
     */
    @GetMapping("getOuterDetail")
    public Result getOuterDetail(@RequestParam String LPN) {
        QueryWrapper<OuterDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("LPN", LPN);
        log.info(LPN);
        OuterDetail outerDetail = outerDetailService.getOne(queryWrapper);
        if (outerDetail == null) {
            return Result.fail("不存在该LPN!");
        }

        return Result.ok(outerDetail);
    }

    /**
     * 扫描物料明细码，获得物料明细信息
     *
     * @param mdmId
     * @return
     */
    @GetMapping("getMaterialDetail")
    public Result getMaterialDetail(Long mdmId) {
        log.info(mdmId.toString());
        MaterialDetailMaintenance mdm = materialDetailMaintenanceService.getById(mdmId);
        if (mdm == null) {
            return Result.fail("没有该编码:" + mdmId);
        }
        return Result.ok(mdm);
    }

    /**
     * （先扫LPN码，查看外层明细信息）点击确认收料
     * 收料单外层明细
     * 状态：未接收-->已接收
     *
     * @return
     */
    @PostMapping("outerDetailReceive")
    public Result outerDetailReceive(@RequestParam String LPN) {
        UpdateWrapper<OuterDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("LPN", LPN);
        log.info(LPN);
        OuterDetail existingRecord = outerDetailService.getOne(updateWrapper);
        // 判断LPN是否已经被接收 或为空
        if (existingRecord != null && existingRecord.getStatus().equals("接收")) {
            return Result.fail(LPN + ":重复接收！");
        } else if (existingRecord == null) {
            return Result.fail("没有该LPN：" + LPN);
        }
        existingRecord.setStatus("接收");
        outerDetailService.updateById(existingRecord);

        // 检查订单是否完成
        log.info("检查订单：" + existingRecord.getReceiveMaterialId());
        orderStatus(existingRecord.getReceiveMaterialId());
        return Result.ok(LPN + ":接收成功");
    }

    /**
     * 扫描订单编码
     * 仓库收料物料
     * 录入仓库名称
     * 状态：未接收 --> 已接收
     *
     * @param mdmID
     * @return
     */
    @PostMapping("materialDetailReceive")
    public Result materialDetailReceive(@RequestParam String storehouseName, Long mdmID) {
        MaterialDetailMaintenance mdm = materialDetailMaintenanceService.getById(mdmID);

        // 判断该物料编码是否已经被接收 或为空
        if (mdm != null && "接收".equals(mdm.getStatus())) {
            return Result.fail(mdmID + ":重复接收！");
        } else if (mdm == null) {
            return Result.fail("没有该编码:" + mdmID);
        }

        mdm.setStorehouseName(storehouseName);
        mdm.setStatus("接收");
        materialDetailMaintenanceService.updateById(mdm);

        // 检查订单是否完成
        log.info("检查订单：" + mdm.getReceiveMaterialId());
        orderStatus(mdm.getReceiveMaterialId());
        return Result.ok(mdmID + ":接收成功");
    }

    /**
     * 每次录入完外层明细信息或者物料明细信息
     * 都进行一次订单状态的判断
     * 如果都完成了则更新收料单状态为：订单完成
     * @param rmId
     */
    public void orderStatus(Long rmId) {

        // 查询 外层明细单 是否完成
        QueryWrapper<OuterDetail> outerDetailQueryWrapper = new QueryWrapper<>();
        outerDetailQueryWrapper.eq("receive_material_id", rmId);
        List<OuterDetail> outerDetails = outerDetailService.list(outerDetailQueryWrapper);
        for (OuterDetail outerDetail : outerDetails) {
            if ("未接收".equals(outerDetail.getStatus())) {
                return ;
            }
        }
        // 查询 物料明细单 是否完成
        QueryWrapper<MaterialDetailMaintenance> mdmQueryWrapper = new QueryWrapper<>();
        mdmQueryWrapper.eq("receive_material_id", rmId);
        List<MaterialDetailMaintenance> mdms = materialDetailMaintenanceService.list(mdmQueryWrapper);
        for (MaterialDetailMaintenance mdm : mdms) {
            if ("未接收".equals(mdm.getStatus())) {
                return ;
            }
        }

        log.info("更新订单");
        // 更新订单状态
        ReceiveMaterial receiveMaterial = receiveMaterialService.getById(rmId);
        log.info(receiveMaterial.toString());
        receiveMaterial.setStatus("订单完成");
        receiveMaterialService.updateById(receiveMaterial);

    }

}
